from django.http import JsonResponse
from django.core.serializers.json import DjangoJSONEncoder


class MyResponse(JsonResponse):
    def __init__(self, data, encoder=DjangoJSONEncoder, safe=True,
                 json_dumps_params=None, **kwargs):
        if not json_dumps_params:
            json_dumps_params = {'ensure_ascii': False}
        super().__init__(data, encoder, safe, json_dumps_params, **kwargs)

    @staticmethod
    def success(data=None, message='success', code=200):
        res = {
            'code': code,
            'message': message,
            'data': data
        }
        return MyResponse(res)

    @staticmethod
    def failure(data, message='failure', code=200):
        res = {
            'code': code,
            'message': message,
            'data': data
        }
        return MyResponse(res)
